package handle

import (
	common "gitee.com/jinmingzhi/cdce-service/api/protos/common/v1alpha"
	cost "gitee.com/jinmingzhi/cdce-service/api/protos/cost/v1alpha"
	exam "gitee.com/jinmingzhi/cdce-service/api/protos/exam/v1alpha"
	participate "gitee.com/jinmingzhi/cdce-service/api/protos/participate/v1alpha"
	"gitee.com/jinmingzhi/cdce-service/internal/model"
	"github.com/golang/protobuf/ptypes"
)

func paysTopbListResp(in []*model.PayList) *cost.PayListResp {
	out := make([]*common.Pay, len(in))
	for i, v := range in {
		out[i] = payTopb(v)
	}

	return &cost.PayListResp{Data: out}
}

func paysTopbComplatedResp(in []*model.PayList) *cost.PayComplatedResp {
	out := make([]*common.Pay, len(in))
	for i, v := range in {
		out[i] = payTopb(v)
	}

	return &cost.PayComplatedResp{Data: out}
}

func payTopb(in *model.PayList) *common.Pay {
	return &common.Pay{
		Id:         in.ID,
		PayNum:     in.PayNum,
		Uid:        in.UID,
		SchoolTime: in.SchoolTime,
		BatchName:  in.BatchName,
		Amount:     in.Amount,
		Status:     in.Status,
	}
}

func modelToPbUser(in *model.User) *common.User {
	out := &common.User{
		Id:                in.ID,
		User:              in.User,
		Gender:            in.Gender,
		RegistratonNumber: in.RegistratonNumber,
		StudentId:         in.StudentID,
		Name:              in.Name,
		ImageUrl:          in.ImageUrl,
	}
	if t, err := ptypes.TimestampProto(in.CreateTime.ToTime()); err == nil {
		out.CreateTime = t
	}
	return out
}

func modelToPbUserDetail(in *model.UserDetail) *common.UserDetail {
	return &common.UserDetail{
		Id:               in.ID,
		Uid:              in.Uid,
		Nation:           in.Nation,
		StudentStatus:    common.StudentStatus(in.StudentStatus),
		OccupationStatus: common.OccupationStatus(in.OccupationStatus),
		CertificateType:  common.CertificateType(in.CertificateType),
		SchoolName:       in.SchoolName,
		CultivationLevel: common.CultivationLevel(in.CultivationLevel),
		IdNum:            in.IDNumber,
	}
}

func modelsToPbScore(in []*model.Score) *exam.ListResp {
	out := make([]*exam.Score, len(in))
	for i, v := range in {
		out[i] = modelToPbScore(v)
	}
	return &exam.ListResp{Data: out}
}

func modelToPbScore(in *model.Score) *exam.Score {
	return &exam.Score{
		BatchName:  in.BatchName,
		CourseName: in.CourseName,
		Result:     in.Result,
	}
}

func modelsToPbSubjectResp(in []*model.Course) *participate.SubjectResp {
	out := make([]*common.Course, len(in))
	for i, v := range in {
		out[i] = modelToPbCourse(v)
	}

	return &participate.SubjectResp{Data: out}
}

func modelToPbCourse(in *model.Course) *common.Course {
	out := &common.Course{
		Id:   in.ID,
		Name: in.CourseName,
	}

	if t, err := ptypes.TimestampProto(in.CreateTime.ToTime()); err == nil {
		out.CreateTime = t
	}
	return out
}

func modelsToPbSchoolResp(in []*model.AreaSchool) *participate.SchoolResp {
	out := make([]*common.AreaSchool, len(in))
	for i, v := range in {
		out[i] = modelToPbAreaSchool(v)
	}
	return &participate.SchoolResp{Data: out}
}

func modelToPbAreaSchool(in *model.AreaSchool) *common.AreaSchool {
	out := &common.AreaSchool{
		Id:            in.ID,
		Rid:           in.RID,
		CourseId:      in.CourseID,
		School:        in.School,
		Status:        in.Status,
		Area:          in.Area,
		CandidatesNum: in.CandidatesNum,
	}

	if t, err := ptypes.TimestampProto(in.CreateTime.ToTime()); err == nil {
		out.CreateTime = t
	}
	return out
}
